package manage.actions.example;

import java.util.ArrayList;
import java.util.List;

import manage.actions.BaseAction;
import manage.commons.ExcelEntry;
import manage.commons.annontations.AccessVerify;
import manage.commons.annontations.RequiredLogin;
import manage.commons.utils.ExcelUtils;
import manage.commons.utils.Pager;
import manage.commons.utils.ServletUtils;
import manage.modules.dept.model.Dept;
import manage.modules.dept.service.DeptService;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

@RequiredLogin
@AccessVerify(-1)
public class ExportExcelAction extends BaseAction {

	private static final long serialVersionUID = 7860999110411782688L;
	private DeptService deptService;
	private Pager<Dept> pager = new Pager<Dept>(5000);
	private Dept dept;
	private String excelFileName;
	private HSSFWorkbook wb;

	@Override
	@Action("export-excel-input")
	public String input() {
		return SUCCESS;
	}

	@Action(value = "export-excel-query", results = {
			@Result(name = SUCCESS, location = "/example/export-excel-list.jsp"),
			@Result(name = "export-excel", type = "excel", params = {
					"excelObject", "wb", "fileName", "${excelFileName}" }) })
	public String query() throws Exception {
		if (ServletUtils.isExportExcel(request)) {
			pager.setPageIndex(0);
			pager.setPageSize(pager.getMaxPageSize());
			List<ExcelEntry> params = new ArrayList<ExcelEntry>();
			params.add(new ExcelEntry("deptId", "部门ID"));
			params.add(new ExcelEntry("deptName", "部门名称"));
			params.add(new ExcelEntry("ability", "部门职能"));
			deptService.query(dept, pager);
			wb = ExcelUtils.generateExcel(pager.getList(), params);
			excelFileName = "dept-ecxel";
			return "export-excel";
		} else {
			deptService.query(dept, pager);
			pager.pagerUtils(pager, getRequest());
		}
		return SUCCESS;
	}

	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}

	public void setPager(Pager<Dept> pager) {
		this.pager = pager;
	}

	public void setDept(Dept dept) {
		this.dept = dept;
	}

	public void setExcelFileName(String excelFileName) {
		this.excelFileName = excelFileName;
	}

	public void setWb(HSSFWorkbook wb) {
		this.wb = wb;
	}

	public String getExcelFileName() {
		return excelFileName;
	}

	public HSSFWorkbook getWb() {
		return wb;
	}

	public Pager<Dept> getPager() {
		return pager;
	}

	public Dept getDept() {
		return dept;
	}
}
